﻿@model BTKERP.MVCWeb.Models.GRNManager

@{
    ViewBag.Title = "GRN Manage";
}
@section AdditionalJS
{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#productTable").dataTable();
            $("#txtUnitCost").validate({
                rules: {
                    tname: { required: true }
                },
                messages: {
                    tname: { required: "Unit Cost requised"}
                }
            });
           
        });

        function addDetailProduct(id,name,unitId, unitName, masterId) {
            $("#addDetail").dialog({ width: "auto" });
            $("#txtProductName").val(name);
            $("#txtProductId").val(id);
            $("#txtUnitName").val(unitName);
            $("#txtUnitId").val(unitId);
            $("#txtGRNMasterId").val(masterId);
            $("#txtUnitCost").focus();
        }

        function editDetail(id, name, unitId, unitName, masterId, qty, unitcost) {
            $("#editDetail").dialog({ width: "auto" });
            $("#txtProductNameEdit").val(name);
            $("#txtProductIdEdit").val(id);
            $("#txtUnitNameEdit").val(unitName);
            $("#txtUnitIdEdit").val(unitId);
            $("#txtGRNMasterIdEdit").val(masterId);
            $("#txtQtyEdit").val(qty);
            $("#txtUnitCostEdit").val(unitcost);
            $("#txtUnitCostEdit").focus();
        }

        function deleteDetail(id, name, unitId, unitName, masterId, qty, unitcost) {
            $("#deleteDetail").dialog({ width: "auto" });
            $("#txtProductNameDelete").val(name);
            $("#txtProductIdDelete").val(id);
            $("#txtUnitNameDelete").val(unitName);
            $("#txtUnitIdDelete").val(unitId);
            $("#txtGRNMasterIdDelete").val(masterId);
            $("#txtQtyDelete").val(qty);
            $("#txtUnitCostDelete").val(unitcost);
            $("#btnCancelDelete").focus();
        }

       function cancelAddDetailProduct(){
           $("#addDetail").dialog('close');
       }
       
       function cancelEditDetail() {
           $("#editDetail").dialog('close');
       }

       function cancelDeleteDetail() {
           $("#deleteDetail").dialog('close');
       }
    </script>

}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<table>
<tr>
<form id="masterForm" action="../Manage" method="post">
<td colspan="2">

<div style="display:none">
    <input id="grnMasterId" name="grnMasterId" value='@Model.Master.Id'/>
    <input id="warehouseId" name="warehouseId" value='@Model.Master.ReceivedWarehouseId'/>
</div>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>GRN General</legend>
        <div class="box box-info">
            <b>Code : </b>@Html.DisplayFor(model => model.Master.GRNCode) &nbsp&nbsp
            <b>GRN Date : </b>@Html.DisplayFor(model => model.Master.GRNDate.Day)<br />
            <b>Supplier : </b> @BTKERP.MVCWeb.AppCode.DataRefHelper.GetSupplierById(Model.Master.SupplierId) &nbsp&nbsp
            <b>Received Warehouse : </b> @BTKERP.MVCWeb.AppCode.DataRefHelper.GetWarehouseById(Model.Master.ReceivedWarehouseId)
        </div>
        <p>
            <input  type="submit" name="submitButton" value="Post" class="btn btn-green"/>
            <input  type="submit" name="submitButton" value="Void" class="btn btn-red" />
        </p>
    </fieldset>
    
}
</td>
</tr>
<tr>
<td>
</form>
<fieldset>
<legend>Product List</legend>
<table id="productTable" class="display stylized">
    <thead>
    <tr>
        <th>
            Code
        </th>
        <th>
            Name
        </th>
        <th>
            Physical Unit
        </th>
        <th></th>
    </tr>
    </thead>
    <tbody>
@foreach (var item in Model.Products)
{
    <tr class="gradeA">
        <td>
            @Html.DisplayFor(modelItem => item.Code)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @BTKERP.MVCWeb.AppCode.DataRefHelper.GetUnitById(item.PhysicalUnitId)
        </td>
        <td>
            <input id="btnAddDetail" type="button" value="Add"  class="btn btn-green" onclick="addDetailProduct('@item.Id','@item.Name','@item.PhysicalUnitId','@BTKERP.MVCWeb.AppCode.DataRefHelper.GetUnitById(item.PhysicalUnitId)','@Model.Master.Id')" />
        </td>
    </tr>
}
</tbody>

</table>
</fieldset>
</td>
<td>
<fieldset>
<legend>Received</legend>
<table id="dt" class="display stylized">
    <tr>
        <th>
            Product
        </th>
        <th>
            Unit
        </th>
        <th>
            Quantity
        </th>
        <th>
            Unit Cost
        </th>
        <th>
            Total
        </th>
        <th></th>
    </tr>

@foreach (var item in Model.Details)
{
    <tr>
        <td>
            @BTKERP.MVCWeb.AppCode.DataRefHelper.GetProductNameById(item.ProductId)

        </td>
        <td>
            @BTKERP.MVCWeb.AppCode.DataRefHelper.GetUnitById(item.UnitId)
        </td>
         <td>
            @Html.DisplayFor(modelItem => item.Quantity)
        </td>
         <td>
            @Html.DisplayFor(modelItem => item.UnitCost)
        </td>
         <td>
            @{
                var qty = item.Quantity;
                var unitCost = item.UnitCost;
                var total = string.Format("{0:0.00}", qty * unitCost);
            }
            @total
        </td>
        <td>
            <input id="btnEditDetail" type="button" value="Change"  class="btn btn-blue" onclick="editDetail('@item.ProductId','@BTKERP.MVCWeb.AppCode.DataRefHelper.GetProductNameById(item.ProductId)','@item.UnitId','@BTKERP.MVCWeb.AppCode.DataRefHelper.GetUnitById(item.UnitId)','@item.GRNMasterId','@item.Quantity','@item.UnitCost')" /> 
            &nbsp&nbsp<input id="btnDeleteDetail" type="button" value="Delete"  class="btn btn-red" onclick="deleteDetail('@item.ProductId','@BTKERP.MVCWeb.AppCode.DataRefHelper.GetProductNameById(item.ProductId)','@item.UnitId','@BTKERP.MVCWeb.AppCode.DataRefHelper.GetUnitById(item.UnitId)','@item.GRNMasterId','@item.Quantity','@item.UnitCost')" /> 
        </td>
    </tr>
}

</table>
</fieldset>
</td>
</tr>
</table>
<div>
    <b>@Html.ActionLink("Back to List", "Index")</b>
</div>

<div id="addDetail" title="Receive Product" style="display:none">
<form id="addDetailForm" action="../AddReceivedProduct" method="post">
<input id="txtGRNMasterId" name="txtGRNMasterId" style="dispaly:none"  />
<input id="txtProductId" name="txtProductId" style="dispaly:none"  />
<input id="txtUnitId" name="txtUnitId" style="dispaly:none"  />
<fieldset>
    <legend>Revceived</legend>
    <b>Name: </b><input type="text" readonly="readonly" id="txtProductName"  /><br />
    <b>Unit : </b><input type="text" readonly="readonly" id="txtUnitName"  /><br />
    <b>Unit Cost : </b><input type="text" name="txtUnitCost" id="txtUnitCost" value="0.00"  /><br />
    <b>Quantity : </b><input type="text" id="txtQty" name="txtQty" value="0"/><br />

</fieldset>
 <input type="submit" id="btnReceive" class="btn btn-blue" value="Reveive" />&nbsp&nbsp&nbsp&nbsp
  <input type="button" class="btn btn-red" value="Cancel"  onclick="cancelAddDetailProduct()"/>
  </form>
</div>

<div id="editDetail" title="Edit Received" style="display:none">
<form id="editDetailForm" action="../EditReceivedProduct" method="post">
<div style="display:none">
<input id="txtGRNMasterIdEdit" name="txtGRNMasterIdEdit" />
<input id="txtProductIdEdit" name="txtProductIdEdit"  />
<input id="txtUnitIdEdit" name="txtUnitIdEdit"  />
</div>
<fieldset>
    <legend>Revceived</legend>
    <b>Name: </b><input type="text" readonly="readonly" id="txtProductNameEdit"  /><br />
    <b>Unit : </b><input type="text" readonly="readonly" id="txtUnitNameEdit"  /><br />
    <b>Unit Cost : </b><input type="text" name="txtUnitCostEdit" id="txtUnitCostEdit" value="0.00"  /><br />
    <b>Quantity : </b><input type="text" id="txtQtyEdit" name="txtQtyEdit" value="0"/><br />

</fieldset>
 <input type="submit"  class="btn btn-green" value="Save" />&nbsp&nbsp&nbsp&nbsp
  <input type="button" class="btn btn-red" value="Cancel"  onclick="cancelEditDetail()"/>
  </form>
</div>

<div id="deleteDetail" title="Delete Received" style="display:none">
<form id="deleteDetailForm" action="../DeleteReceivedProduct" method="post">
<div style="display:none">
<input id="txtGRNMasterIdDelete" name="txtGRNMasterIdDelete" />
<input id="txtProductIdDelete" name="txtProductIdDelete"  />
<input id="txtUnitIdDelete" name="txtUnitIdDelete"  />
</div>
<fieldset>
    <legend>Confirm to delete?</legend>
    <b>Name: </b><input type="text" readonly="readonly" id="txtProductNameDelete"  /><br />
    <b>Unit : </b><input type="text" readonly="readonly" id="txtUnitNameDelete"  /><br />
    <b>Unit Cost : </b><input type="text" readonly="readonly" name="txtUnitCostDelete" id="txtUnitCostDelete"/><br />
    <b>Quantity : </b><input type="text" readonly="readonly" id="txtQtyDelete" name="txtQtyDelete"/><br />

</fieldset>
 <input type="submit"  class="btn btn-red" value="Delete" />&nbsp&nbsp&nbsp&nbsp
  <input type="button" id="btnCancelDelete" class="btn btn-blue" value="Cancel"  onclick="cancelDeleteDetail()"/>
  </form>
</div>

